package servlet.customeraction;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import service.IServiceManager;
import service.exception.ServiceException;

import bean.Customer;
import bean.User;
import bean.UserProfile;
import business.user.UserManagement;

public class RegisterAction extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7452997816861585945L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String errorMsg=null;
		HttpSession session=request.getSession();
		if (session.getAttribute("register_error")!=null) {
			session.removeAttribute("register_error");
		}	
		Customer customerA = new Customer();
		User userA = new User();
		//Check account field
		if (request.getParameter("userAccount")==null||request.getParameter("userAccount")=="") {
			errorMsg="Please fill in 'Account' field";
			session.setAttribute("register_error", errorMsg);
			response.sendRedirect("register.jsp");
			return;
		}else {
			 String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";  
			 Pattern regex = Pattern.compile(check);  
			 Matcher matcher = regex.matcher(request.getParameter("userAccount"));
			 boolean isMatched = matcher.matches();  
			 if (isMatched) {
				 userA.setAccount(request.getParameter("userAccount"));
			 }else {
				errorMsg="The 'Account' field needs to be a correct e-mail address";
				session.setAttribute("register_error", errorMsg);
				response.sendRedirect("register.jsp");
				return;
			}
			
		}
		//Check name field
		if (request.getParameter("customerName")==null||request.getParameter("customerName")=="") {
			errorMsg="Please fill in 'Name' field";
			session.setAttribute("register_error", errorMsg);
			response.sendRedirect("register.jsp");
			return;
		}else {
			customerA.setName(request.getParameter("customerName"));
		}
		//check address field
		if (request.getParameter("customerAddress")==null||request.getParameter("customerAddress")=="") {
			errorMsg="Please fill in 'Address' field";
			session.setAttribute("register_error", errorMsg);
			response.sendRedirect("register.jsp");
			return;
		}else {
			customerA.setAddress(request.getParameter("customerAddress"));
		}
		//Check telephone field
		if (request.getParameter("customerTelephone")==null||request.getParameter("customerTelephone")=="") {
			errorMsg="Please fill in 'Telephone' field";
			session.setAttribute("register_error", errorMsg);
			response.sendRedirect("register.jsp");
			return;
		}else {
			 String check = "^[0-9]+$";  
			 Pattern regex = Pattern.compile(check);  
			 Matcher matcher = regex.matcher(request.getParameter("customerTelephone"));
			 boolean isMatched = matcher.matches(); 
			 if (isMatched) {
				 customerA.setTelephone(request.getParameter("customerTelephone"));
			 }else {
				   errorMsg="The 'Telephone' field should be filled in with numbers";
					session.setAttribute("register_error", errorMsg);
					response.sendRedirect("register.jsp");
					return;
			}						
		}
		//Check password field		
		if (request.getParameter("userPassword")==null||request.getParameter("userPassword")=="") {
			errorMsg="Please fill in 'Password' field";
			session.setAttribute("register_error", errorMsg);
			response.sendRedirect("register.jsp");
			return;
		}else {
			userA.setPassword(request.getParameter("userPassword"));
		}
		//Check confirm password field
		if (request.getParameter("userConfirmPasword")==null||request.getParameter("userConfirmPasword")=="") {
			errorMsg="Please fill in 'ConfirmPassword' field";
			session.setAttribute("register_error", errorMsg);
			response.sendRedirect("register.jsp");
			return;
		}else {
			if (!request.getParameter("userPassword").equals(request.getParameter("userConfirmPasword"))) {
				errorMsg="The 'ConfirmPassword' field is not the same as 'Password' field";
				session.setAttribute("register_error", errorMsg);
				response.sendRedirect("register.jsp");
				return;
			}
		}
		
		UserProfile customer = new UserProfile(userA, customerA);
		IServiceManager serviceMgn = new UserManagement(request);
		try {
			customer = serviceMgn.execute(
					UserManagement.SERVICE_REGISTER_CUSTOMER, customer);	
			session.setAttribute("register_done", "Register successfully");
			response.sendRedirect("newlogin.jsp");
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			response.sendRedirect("register.jsp");
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
		
}
